package com.jiang.jixiang.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.BankListBean;
import com.jiang.jixiang.databinding.FragmentBankcardBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.ui.adapter.MyCardAdapter;
import com.ws.universal.tools.base.BaseFragment;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BankCardFragment extends BaseFragment implements NetWorkListener {
    private MyCardAdapter myCardAdapter;
    private BankListBean bankListBean;
    private FragmentBankcardBinding binding;

    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentBankcardBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcyData.setLayoutManager(manager);
        myCardAdapter = new MyCardAdapter();
        binding.rcyData.setAdapter(myCardAdapter);

        myCardAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                showProgressDialog(getContext(), false);
                unBindCrad(myCardAdapter.getData().get(position).card_id);
            }
        });
    }

    @Override
    public void autoData() {

    }

    private void unBindCrad(String card_id) {
        Map<String, String> params = new HashMap<>();
        params.put("card_id", card_id);
        okHttpModel.post(ApiUrl.unBindCard_Api, params, ApiUrl.unBindCard_Api_ID, this);
    }

    private void loadData(String country) {
        Map<String, String> params = new HashMap<>();
        params.put("country", country);
        //okHttpModel.post(ApiUrl.paymentBankList_Api, params, ApiUrl.paymentBankList_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");

        switch (id) {
            /*case ApiUrl.paymentBankList_Api_ID:
                bankListBean = GsonUtils.parseJObject(jsonData, BankListBean.class);
                if (bankListBean != null) {
                    myCardAdapter.setList(bankListBean.data);
                }
                if (myCardAdapter.getData().size() > 0) {
                    binding.llBindBank.setVisibility(View.GONE);
                } else {
                    binding.llBindBank.setVisibility(View.VISIBLE);
                }
                break;*/
        }
    }


    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());

    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }
}
